<template>
  <div class="preview">
    <el-dialog title="题目预览" :visible.sync="dialogFormVisible" @close="dialogFormH">
      <div>
        <el-row>
          <el-col :span="6">【题型】：{{formatterType(formBase.questionType)}}</el-col>
          <el-col :span="6">【编号】：{{formBase.id}}</el-col>
          <el-col :span="6">【难度】：{{formatterDiff(formBase.difficulty)}}</el-col>
          <el-col :span="6">【标签】：{{formBase.tags}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="6">【学科】：{{formBase.subjectName}}</el-col>
          <el-col :span="6">【目录】：{{formBase.directoryName}}</el-col>
          <el-col :span="6">【方向】：{{formBase.direction}}</el-col>
        </el-row>
      </div>
      <hr>
      <div>
        【题干】：
        <p v-html="formBase.question" style="color:blue;"></p>
        {{formatterType(formBase.questionType)}} 选项：(以下选中的选项为正确答案)
        <div v-if="formBase.questionType==2">
          <el-checkbox
            v-model="!!item.isRight"
            :label="item.id"
            :key="item.id"
            v-for="item in formBase.options"
          >{{item.code+'. '+item.title}}</el-checkbox>
        </div>
        <div v-if="formBase.questionType==1">
          <el-radio
            :value="radio"
            :label="item.isRight"
            :key="item.id"
            v-for="item in formBase.options"
          >{{item.code+'. '+item.title}}</el-radio>
        </div>
      </div>
      <hr>
      <div>
        【参考答案】：
        <el-button type="danger" @click="show=true">视频答案预览</el-button>
        <video
          v-if="show"
          height="240px"
          width="320px"
          :src="formBase.videoURL"
          autoplay
          muted
          controls
        ></video>
      </div>
      <hr>
      <div class="box">【答案解析】：{{formatterQuestion(formBase.answer)}}</div>
      <hr>
      <div class="box">【题目备注】：{{formBase.remarks}}</div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogFormH">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { questionType, difficulty, direction } from '@/api/hmmm/constants'
export default {
  props: {
    formBase: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      show: false,
      radio: 1
    }
  },
  methods: {
    // 弹层显示
    dialogFormV(id) {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH() {
      this.show = false
      this.dialogFormVisible = false
    },
    // 格式化题型
    formatterType(val) {
      const item = questionType.find(item => item.value == val)
      return item ? item.label : ''
    },
    // 格式化难度
    formatterDiff(val) {
      const item = difficulty.find(item => item.value == val)
      return item ? item.label : ''
    },
    // 格式化答案解析
    formatterQuestion(htmlStr) {
      return htmlStr
        .replace(/<(style|script|iframe)[^>]*?>[\s\S]+?<\/\1\s*>/gi, '')
        .replace(/<[^>]+?>/g, '')
        .replace(/\s+/g, ' ')
        .replace(/ /g, ' ')
        .replace(/>/g, ' ')
    }
  }
}
</script>
<style lang="less" scoped>
::v-deep .el-dialog {
  width: 900px;
  .el-dialog__footer {
    text-align: right;
  }
  .el-dialog__body {
    padding: 20px;
  }
}
.el-col {
  padding: 10px 0;
}
.el-checkbox,
.el-radio {
  display: block;
  margin: 10px;
}
video {
  display: block;
  padding: 8px 0;
}
.box {
  padding: 8px 0;
}
</style>
